package com.pine.web.pineframework.controller;

import com.pine.common.beans.ResultVo;
import com.pine.common.beans.pineframework.request.*;
import com.pine.common.beans.pineframework.response.SysUserCurrentInfoResponse;
import com.pine.common.beans.pineframework.response.SysUserQueryResponse;
import com.pine.common.beans.request.BatchIdRequest;
import com.pine.common.beans.request.IdRequest;
import com.pine.common.beans.request.PageRequest;
import com.pine.common.beans.response.DeleteResponse;
import com.pine.common.beans.response.PageResponse;
import com.pine.common.systemlog.annotation.SysLog;
import com.pine.manager.pineframework.trigger.SysUserTriggerManager;
import com.pine.service.pineframework.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 系统用户前端控制器 todo 导入导出、模版等功能待完善
 * </p>
 *
 * @author pine manage
 * @since 2024-08-14
 */
@Tag(name = "系统用户前端控制器")
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;

    /**
     * 分页查询
     *
     * @param request 请求
     * @return 分页响应
     */
    @Operation(summary = "分页查询")
    @PostMapping("/pages")
    @SysLog("分页查询")
    public ResultVo<PageResponse<SysUserQueryResponse>> listPages(@RequestBody @Valid PageRequest<SysUserQueryRequest> request) {
        request.defaultPage();
        return ResultVo.success(sysUserService.listPages(request));
    }

    /**
     * 查询信息（不分页）
     *
     * @param request 请求
     * @return PageResponse 响应
     */
    @Operation(summary = "查询信息（不分页）")
    @PostMapping("/list")
    @SysLog("查询信息（不分页）")
    public ResultVo<List<SysUserQueryResponse>> list(@RequestBody @Valid SysUserQueryRequest request) {
        return ResultVo.success(sysUserService.list(request));
    }

    /**
     * 查询详细信息
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/detail")
    @Operation(summary = "查询详细信息")
    @SysLog("查询详细信息")
    public ResultVo<SysUserQueryResponse> getDetail(@RequestBody @Valid IdRequest<Long> request) {
        return ResultVo.success(sysUserService.getOneByUniqueKey(request.getId(), "id"));
    }

    /**
     * 查询当前用户信息
     *
     * @return 响应
     */
    @PostMapping(value = "/current")
    @Operation(summary = "查询当前用户信息")
    @SysLog("查询当前用户信息")
    public ResultVo<SysUserCurrentInfoResponse> getCurrentUserInfo() {
        return ResultVo.success(sysUserService.getCurrentUserInfo());
    }

    /**
     * 新增
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/save")
    @Operation(summary = "新增用户")
    @SysLog("新增用户")
    public ResultVo<Boolean> save(@RequestBody @Valid SysUserSaveOrUpdateRequest request) {
        Boolean triggerResult = SysUserTriggerManager.saveOrUpdate(request);
        return ResultVo.success(triggerResult);
    }

    /**
     * 更新
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/update")
    @Operation(summary = "更新用户")
    @SysLog("更新用户")
    public ResultVo<Boolean> update(@RequestBody @Valid SysUserSaveOrUpdateRequest request) {
        Boolean triggerResult = SysUserTriggerManager.saveOrUpdate(request);
        return ResultVo.success(triggerResult);
    }

    /**
     * 删除
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/delete")
    @Operation(summary = "删除用户")
    @SysLog("删除用户")
    public ResultVo<DeleteResponse> delete(@RequestBody @Valid BatchIdRequest<Long> request) {
        DeleteResponse triggerResult = SysUserTriggerManager.delete(request);
        return ResultVo.success(triggerResult);
    }

    /**
     * 重置密码
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/resetPassword")
    @Operation(summary = "重置密码")
    @SysLog("重置密码")
    public ResultVo<Boolean> resetPassword(@RequestBody @Valid SysUserResetPasswordRequest request) {
        Boolean triggerResult = SysUserTriggerManager.resetPassword(request);
        return ResultVo.success(triggerResult);
    }

    /**
     * 修改状态
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/updateStatus")
    @Operation(summary = "修改状态")
    @SysLog("修改状态")
    public ResultVo<Boolean> updateStatus(@RequestBody @Valid SysUserUpdateStatusRequest request) {
        Boolean triggerResult = SysUserTriggerManager.updateStatus(request);
        return ResultVo.success(triggerResult);
    }

    /**
     * 分配角色
     *
     * @param request 请求
     * @return 响应
     */
    @PostMapping(value = "/assignRole")
    @Operation(summary = "分配角色")
    @SysLog("分配角色")
    public ResultVo<Boolean> assignRole(@RequestBody @Valid SysUserAssignRoleRequest request) {
        Boolean triggerResult = SysUserTriggerManager.assignRole(request);
        return ResultVo.success(triggerResult);
    }
}
